{%- macro translate(key, i18n="", default="") %}
{{- i18n[key] | default(value=default) | safe -}}
{%- endmacro %}

{%- macro page_listing(page, config) %}
{%- set uglyurls = config.extra.uglyurls | default(value=false) -%}
{%- if config.extra.search_library %}{%- if config.extra.search_library == "offline" %}{% set uglyurls = true %}{% endif %}{% endif %}
        <div class="tpad">
          <a href="{{ page.permalink | safe}}{%- if uglyurls %}index.html{%- endif %}">{{ page.title }}</a>
        </div>
{%- endmacro page_listing %}


{%- macro series_link(inner, page, config) %}
{%- set uglyurls = config.extra.uglyurls | default(value=false) -%}
{%- if config.extra.search_library %}{%- if config.extra.search_library == "offline" %}{% set uglyurls = true %}{% endif %}{% endif %}
  {%- if inner.content == page.content %}
      <a class="scur" href="{{ inner.permalink | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ inner.title }}</a><br>
    {% else %}
      <a href="{{ inner.permalink | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ inner.title }}</a><br>
  {%- endif %}
{%- endmacro %}


{%- macro title_post(page, config) %}
{%- set uglyurls = config.extra.uglyurls | default(value=false) -%}
{%- if config.extra.search_library %}{%- if config.extra.search_library == "offline" %}{% set uglyurls = true %}{% endif %}{% endif %}
      <h1><a href="{{ page.permalink | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ page.title }}</a></h1>
{%- endmacro title_post %}



{%- macro meta_post(page, config) %}
{# Load current language i18n data from .toml files in user's '/i18n' folder, use theme as fallback. #}
{%- set i18n = load_data(path="i18n/" ~ lang ~ '.toml', required=false) -%}
{%- if not i18n -%}{%- set i18n = load_data(path="themes/abridge/i18n/" ~ lang ~ ".toml", required=false) -%}{%- endif %}

{%- set uglyurls = config.extra.uglyurls | default(value=false) -%}
{%- if config.extra.search_library %}{%- if config.extra.search_library == "offline" %}{% set uglyurls = true %}{% endif %}{% endif %}
      <span{%- if config.extra.meta_post.size %} class="{{ config.extra.meta_post.size }}"{% endif %}>

        {#- Author #}
        {%- if page.taxonomies.authors and config.taxonomies %}
          {%- for author in page.taxonomies.authors %}
          {%- if author_flag %}, {% endif %}
          <a href="{{ get_taxonomy_url(kind='authors', name=author) | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ author }}</a>
          {%- set_global author_flag = true %}
          {%- endfor %}
        {%- elif page.extra.authors and config.extra.meta_post.author %}
          {%- if config.extra.icon_author %}<i class="{{ config.extra.icon_author }}"></i> {% endif %}
          {%- for author in page.extra.authors %}
          {%- if author_flag %}, {% endif %} {{ author }}
          {%- set_global author_flag = true %}
          {%- endfor %}
        {%- elif config.extra.author and config.extra.meta_post.author %}
          {%- if config.extra.icon_author %}<i class="{{ config.extra.icon_author }}"></i> {% endif %} {{ config.extra.author }}
        {%- endif %}

          {#- divider #}
          {%- if config.extra.meta_post.author %}
          {%- if config.extra.meta_post.readtime or config.extra.meta_post.date or config.extra.meta_post.updated or config.extra.meta_post.categories_tags %}
          {%- if config.extra.meta_post.divider %}<span class="hpad">{{ config.extra.meta_post.divider | safe }}</span>{%- else %} <span class="rpad"></span> {%- endif %}
          {%- endif %}
          {%- endif %}

        {#- ReadTime #}
          {%- if config.extra.meta_post.readtime %} {%- if config.extra.icon_read %}<i class="{{ config.extra.icon_read }}"></i> {% endif %} {{ page.reading_time }} {{ config.extra.meta_post.readstring | default(value="min") }}
          {#- divider #}
          {%- if config.extra.meta_post.date or config.extra.meta_post.updated or config.extra.meta_post.categories_tags %}
          {%- if config.extra.meta_post.divider %}<span class="hpad">{{ config.extra.meta_post.divider | safe }}</span>{%- else %} <span class="rpad"></span> {%- endif %}{%- endif %}{%- endif %}

        {#- Date #}
          {%- if config.extra.meta_post.date %} {%- if config.extra.icon_date %}<i class="{{ config.extra.icon_date }}"></i> {% endif %}
          {%- set lang_locale = macros::translate(key='date_locale', default='en_US', i18n=i18n) -%}
          {%- set page_date = page.date | date(format="%B %d, %Y", locale=lang_locale) -%}
          {%- if page_date %} {{ page_date }} {%- else %} {{ page.date | date(format="%B %d, %Y") }} {%- endif %}
          {#- divider #}
          {%- if config.extra.meta_post.updated or config.extra.meta_post.categories_tags %}
          {%- if config.extra.meta_post.divider %}<span class="hpad">{{ config.extra.meta_post.divider | safe }}</span>{%- else %} <span class="rpad"></span> {%- endif %}{%- endif %}{% endif %}

        {#- Updated #}
          {%- if config.extra.meta_post.updated and page.updated %} {%- if config.extra.icon_date %}<i class="{{ config.extra.icon_date }}"></i> {% endif -%}
          {{ macros::translate(key="Updated", default="Updated", i18n=i18n) }}:
          {%- set lang_locale = macros::translate(key='date_locale', default='en_US', i18n=i18n) -%}
          {%- set page_updated = page.updated | date(format="%B %d, %Y", locale=lang_locale) -%}
          {%- if page_updated %} {{ page_updated }} {%- else %} {{ page.updated | date(format="%B %d, %Y") }} {%- endif %}
          {#- divider #}
          {%- if config.extra.meta_post.categories_tags %}
          {%- if config.extra.meta_post.divider %}<span class="hpad">{{ config.extra.meta_post.divider | safe }}</span>{%- else %} <span class="rpad"></span> {%- endif %}{%- endif %}{% endif %}

        {#- Categories #}
          {%- if page.taxonomies.categories %} {%- if config.extra.icon_info %}<i class="{{ config.extra.icon_info }}"></i> {% endif %} [{% for cat in page.taxonomies.categories %}<a href="{{ get_taxonomy_url(kind='categories', name=cat, lang=lang) | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ cat }}</a>{%- if not loop.last %}, {% endif %}{% endfor %}]{% endif %}

        {#- Tags #}
          {%- if page.taxonomies.tags %} {%- for tag in page.taxonomies.tags %} #<a href="{{ get_taxonomy_url(kind='tags', name=tag, lang=lang) | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ tag }}</a> {% endfor -%}{%- endif %}</span>
{%- endmacro meta_post %}



{%- macro title_index(page, config) %}
{%- set uglyurls = config.extra.uglyurls | default(value=false) -%}
{%- if config.extra.search_library %}{%- if config.extra.search_library == "offline" %}{% set uglyurls = true %}{% endif %}{% endif %}
        <big>{% if config.extra.title_size_index %}<span class="{{ config.extra.title_size_index }}">{% endif %}<a href="{{ page.permalink | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ page.title }}</a>{% if config.extra.title_size_index %}</span>{% endif %}</big>
{%- endmacro title_index %}



{%- macro meta_index(page, config) %}
{# Load current language i18n data from .toml files in user's '/i18n' folder, use theme as fallback. #}
{%- set i18n = load_data(path="i18n/" ~ lang ~ '.toml', required=false) -%}
{%- if not i18n -%}{%- set i18n = load_data(path="themes/abridge/i18n/" ~ lang ~ ".toml", required=false) -%}{%- endif %}
{%- set uglyurls = config.extra.uglyurls | default(value=false) -%}
{%- if config.extra.search_library %}{%- if config.extra.search_library == "offline" %}{% set uglyurls = true %}{% endif %}{% endif %}
        <span{%- if config.extra.meta_index.size %} class="{{ config.extra.meta_index.size }}"{% endif %}>

        {#- Author #}
        {%- if page.taxonomies.authors and config.taxonomies %}
          {%- for author in page.taxonomies.authors %}
          {%- if author_flag %}, {% endif %}
          <a href="{{ get_taxonomy_url(kind='authors', name=author, lang=lang) | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ author }}</a>
          {%- set_global author_flag = true %}
          {%- endfor %}
        {%- elif page.extra.authors and config.extra.meta_index.author %}
          {%- if config.extra.icon_author %}<i class="{{ config.extra.icon_author }}"></i> {% endif %}
          {%- for author in page.extra.authors %}
          {%- if author_flag %}, {% endif %} {{ author }}
          {%- set_global author_flag = true %}
          {%- endfor %}
        {%- elif config.extra.author and config.extra.meta_index.author %}
          {%- if config.extra.icon_author %}<i class="{{ config.extra.icon_author }}"></i> {% endif %} {{ config.extra.author }}
        {%- endif %}

          {#- divider #}
          {%- if config.extra.meta_index.author %}
          {%- if config.extra.meta_index.readtime or config.extra.meta_index.date or config.extra.meta_index.updated or config.extra.meta_index.categories_tags %}
          {%- if config.extra.meta_index.divider %}<span class="hpad">{{ config.extra.meta_index.divider | safe }}</span>{%- else %} <span class="rpad"></span> {%- endif %}
          {%- endif %}
          {%- endif %}

        {#- ReadTime #}
          {%- if config.extra.meta_index.readtime %} {%- if config.extra.icon_read %}<i class="{{ config.extra.icon_read }}"></i> {% endif %} {{ page.reading_time }} {{ config.extra.meta_index.readstring | default(value="min") }}
          {#- divider #}
          {%- if config.extra.meta_index.date or config.extra.meta_index.updated or config.extra.meta_index.categories_tags %}
          {%- if config.extra.meta_index.divider %}<span class="hpad">{{ config.extra.meta_index.divider | safe }}</span>{%- else %} <span class="rpad"></span> {%- endif %}{%- endif %}{%- endif %}

        {#- Date #}
          {%- if config.extra.meta_index.date %} {%- if config.extra.icon_date %}<i class="{{ config.extra.icon_date }}"></i> {% endif %}
          {%- set lang_locale = macros::translate(key='date_locale', default='en_US', i18n=i18n) -%}
          {%- set page_date = page.date | date(format="%B %d, %Y", locale=lang_locale) -%}
          {%- if page_date %} {{ page_date }} {%- else %} {{ page.date | date(format="%B %d, %Y") }} {%- endif %}
          {#- divider #}
          {%- if config.extra.meta_index.updated or config.extra.meta_index.categories_tags %}
          {%- if config.extra.meta_index.divider %}<span class="hpad">{{ config.extra.meta_index.divider | safe }}</span>{%- else %} <span class="rpad"></span> {%- endif %}{%- endif %}{% endif %}

        {#- Updated #}
          {%- if config.extra.meta_index.updated and page.updated %} {%- if config.extra.icon_date %}<i class="{{ config.extra.icon_date }}"></i> {% endif -%}
          {{ macros::translate(key="Updated", default="Updated", i18n=i18n) }}:
          {%- set lang_locale = macros::translate(key='date_locale', default='en_US', i18n=i18n) -%}
          {%- set page_updated = page.updated | date(format="%B %d, %Y", locale=lang_locale) -%}
          {%- if page_updated %} {{ page_updated }} {%- else %} {{ page.updated | date(format="%B %d, %Y") }} {%- endif %}
          {#- divider #}
          {%- if config.extra.meta_index.categories_tags %}
          {%- if config.extra.meta_index.divider %}<span class="hpad">{{ config.extra.meta_index.divider | safe }}</span>{%- else %} <span class="rpad"></span> {%- endif %}{%- endif %}{% endif %}

        {#- Categories #}
          {%- if page.taxonomies.categories %} {%- if config.extra.icon_info %}<i class="{{ config.extra.icon_info }}"></i> {% endif %} [{% for cat in page.taxonomies.categories %}<a href="{{ get_taxonomy_url(kind='categories', name=cat, lang=lang) | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ cat }}</a>{%- if not loop.last %}, {% endif %}{% endfor %}]{% endif %}

        {#- Tags #}
          {%- if page.taxonomies.tags %} {%- for tag in page.taxonomies.tags %} #<a href="{{ get_taxonomy_url(kind='tags', name=tag, lang=lang) | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ tag }}</a> {% endfor -%}{%- endif %}</span>
{%- endmacro meta_index %}



{%- macro footer(page, config) %}
{%- set uglyurls = config.extra.uglyurls | default(value=false) -%}
{%- if config.extra.search_library %}{%- if config.extra.search_library == "offline" %}{% set uglyurls = true %}{% endif %}{% endif %}
      {#- prev/next content page nextprev title pagination #}
      {%- if not config.extra.hide_page_nextprev_titles | default(value=false) %}
      {%- if page.lower or page.higher %}
      <nav>
        <div>
          {%- if page.lower %}
          <a href="{{ page.lower.permalink | safe }}{%- if uglyurls %}index.html{%- endif %}">&#8249; {{ page.lower.title | truncate(length=100) }}</a>
          {%- endif %}
        </div>
        <div>
          {%- if page.higher %}
          <a href="{{ page.higher.permalink | safe }}{%- if uglyurls %}index.html{%- endif %}"> {{ page.higher.title | truncate(length=100) }} &#8250;</a>
          {%- endif %}
        </div>
      </nav>
      {%- endif %}
      {%- endif %}
{%- endmacro footer %}



{%- macro pagination(paginator, config)%}
{%- set uglyurls = config.extra.uglyurls | default(value=false) -%}
{%- if config.extra.search_library %}{%- if config.extra.search_library == "offline" %}{% set uglyurls = true %}{% endif %}{% endif %}
  {%- if paginator.number_pagers > 1 %}
            {%- if paginator.number_pagers <= 5 %}  {# 5 or less pager is simple, just display all 5 #}
                {%- set start = 1 %}
                {%- set end = paginator.number_pagers %}
            {%- endif %}

            {%- if paginator.number_pagers > 5 %}

                {%- if paginator.current_index >= 3 %}                {# Check if the first number will be 1 or not #}
                    {%- set start = paginator.current_index - 2 %}    {# Set the start number #}
                    {%- if paginator.number_pagers - paginator.current_index < 2 %}  {# the last two pages your no longer centered with equal pages start and end #}
                        {%- set difference = paginator.number_pagers - paginator.current_index %}
                        {%- set compensation =  1 - difference %}
                        {%- set start = paginator.current_index - 3 - compensation %}  {# however many pages are missing on the end, add offset to start #}
                    {%- endif %}
                {%- else %}
                    {%- set start = 1 %}
                {%- endif %}

                {%- if paginator.number_pagers >= paginator.current_index + 2 %}  {# Check if the last page is beyond our range #}
                    {%- set end = paginator.current_index + 2 %}  {# End is beyond our range so we can safely set it to our range #}
                    {%- if paginator.current_index <= 3 %}  {# check offset start, if on page 1 or 2 then set the end to page 5 #}
                        {%- set end = 5 %}
                    {%- endif %}
                {%- else %}
                    {%- set end = paginator.number_pagers %}
                {%- endif %}
            {%- endif %}
      <nav class="vpad">
        {%- set icon_first=config.extra.icon_first | default(value="svgs svgh angll") -%}
        {%- set icon_prev=config.extra.icon_prev | default(value="svgs svgh angl") -%}
        {%- set icon_next=config.extra.icon_next | default(value="svgs svgh angr") -%}
        {%- set icon_last=config.extra.icon_last | default(value="svgs svgh angrr") -%}
        {%- if paginator.previous -%}
          <a class="on outp" href="{{ paginator.first | safe }}{%- if uglyurls %}index.html{%- endif %}" title="First Page">
          {%- if icon_first -%}<i class="{{ icon_first }}" alt="First Page"></i>
          {%- else -%}&#171;
          {%- endif -%}
          </a>
        {%- else -%}
          <span class="off outp">
          {%- if icon_first -%}<i class="{{ icon_first }}" alt="First Page"></i>
          {%- else -%}&#171;
          {%- endif -%}
          </span>
        {%- endif -%}

        {%- if paginator.previous -%}
          <a class="on outp" href="{{ paginator.previous | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Previous Page">
          {%- if icon_prev -%}<i class="{{ icon_prev }}" alt="Previous Page"></i>
          {%- else -%}&#8249;
          {%- endif -%}
          </a>
        {%- else -%}
          <span class="off outp">
          {%- if icon_prev -%}<i class="{{ icon_prev }}" alt="Previous Page"></i>
          {%- else -%}&#8249;
          {%- endif -%}
          </span>
        {%- endif -%}

        {%- for i in range(start=start, end=end+1) -%}
            {%- if i == paginator.current_index -%}
            <a class="inp on cnav" href="#">{{i}}</a>
            {%- elif i > 1 -%}
            <a class="inp on" href="{{ paginator.base_url | safe }}{{ i ~ '/' }}{%- if uglyurls %}index.html{%- endif %}">{{i}}</a>
            {%- else -%}
            <a class="inp on" href="{{ paginator.first | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ i }}</a>
            {%- endif -%}
        {%- endfor -%}

        {%- if paginator.next -%}
          <a class="on outp" href="{{ paginator.next | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Next Page">
          {%- if icon_next -%}<i class="{{ icon_next }}" alt="Next Page"></i>
          {%- else -%}&#8250;
          {%- endif -%}
          </a>
        {%- else -%}
          <span class="off outp">
          {%- if icon_next -%}<i class="{{ icon_next }}" alt="Next Page"></i>
          {%- else -%}&#8250;
          {%- endif -%}
          </span>
        {%- endif -%}

        {%- if paginator.next -%}
          <a class="on outp" href="{{ paginator.last | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Last Page">
          {%- if icon_last -%}<i class="{{ icon_last }}" alt="Last Page"></i>
          {%- else -%}&#187;
          {%- endif -%}
          </a>
        {%- else -%}
          <span class="off outp">
          {%- if icon_last -%}<i class="{{ icon_last }}" alt="Last Page"></i>
          {%- else -%}&#187;
          {%- endif -%}
          </span>
        {%- endif -%}
      </nav>
  {%- endif %}
{%- endmacro %}



{%- macro numpagination(paginator, config)%}
{%- set uglyurls = config.extra.uglyurls | default(value=false) -%}
{%- if config.extra.search_library %}{%- if config.extra.search_library == "offline" %}{% set uglyurls = true %}{% endif %}{% endif %}
  {%- if paginator.number_pagers > 1 %}
            {%- if paginator.number_pagers <= 5 %}  {# 5 or less pager is simple, just display all 5 #}
                {%- set start = 1 %}
                {%- set end = paginator.number_pagers %}
            {%- endif %}

            {%- if paginator.number_pagers > 5 %}

                {%- if paginator.current_index >= 3 %}                {# Check if the first number will be 1 or not #}
                    {%- set start = paginator.current_index - 2 %}    {# Set the start number #}
                    {%- if paginator.number_pagers - paginator.current_index < 2 %}  {# the last two pages your no longer centered with equal pages start and end #}
                        {%- set difference = paginator.number_pagers - paginator.current_index %}
                        {%- set compensation =  1 - difference %}
                        {%- set start = paginator.current_index - 3 - compensation %}  {# however many pages are missing on the end, add offset to start #}
                    {%- endif %}
                {%- else %}
                    {%- set start = 1 %}
                {%- endif %}

                {%- if paginator.number_pagers >= paginator.current_index + 2 %}  {# Check if the last page is beyond our range #}
                    {%- set end = paginator.current_index + 2 %}  {# End is beyond our range so we can safely set it to our range #}
                    {%- if paginator.current_index <= 3 %}  {# check offset start, if on page 1 or 2 then set the end to page 5 #}
                        {%- set end = 5 %}
                    {%- endif %}
                {%- else %}
                    {%- set end = paginator.number_pagers %}
                {%- endif %}
            {%- endif %}
      <nav class="vpad">
        {%- if paginator.current_index > 6 and paginator.number_pagers > 7 %}
          <a class="inp on" href="{{ paginator.first | safe }}{%- if uglyurls %}index.html{%- endif %}" title="First Page">1</a>
          <a class="inp on" href="{{ paginator.base_url | safe }}{{ 2 ~ '/' }}{%- if uglyurls %}index.html{%- endif %}" title="Page 2">2</a>
          <span class="dis outp">...</span>
        {%- elif paginator.current_index > 5 and paginator.number_pagers > 7 %}
          <a class="inp on" href="{{ paginator.first | safe }}{%- if uglyurls %}index.html{%- endif %}" title="First Page">1</a>
          <a class="inp on" href="{{ paginator.base_url | safe }}{{ 2 ~ '/' }}{%- if uglyurls %}index.html{%- endif %}" title="Page 2">2</a>
          <a class="inp on" href="{{ paginator.base_url | safe }}{{ 3 ~ '/' }}{%- if uglyurls %}index.html{%- endif %}" title="Page 3">3</a>
        {%- elif paginator.current_index > 4 and paginator.number_pagers > 6 %}
          <a class="inp on" href="{{ paginator.first | safe }}{%- if uglyurls %}index.html{%- endif %}" title="First Page">1</a>
          <a class="inp on" href="{{ paginator.base_url | safe }}{{ 2 ~ '/' }}{%- if uglyurls %}index.html{%- endif %}" title="Page 2">2</a>
        {%- elif paginator.current_index > 3 and paginator.number_pagers > 5 -%}
          <a class="inp on" href="{{ paginator.first | safe }}{%- if uglyurls %}index.html{%- endif %}" title="First Page">1</a>
        {%- endif -%}

        {%- for i in range(start=start, end=end+1) -%}
            {%- if i == paginator.current_index -%}
            <a class="inp on cnav" href="#">{{i}}</a>
            {%- elif i > 1 -%}
            <a class="inp on" href="{{ paginator.base_url | safe }}{{ i ~ '/' }}{%- if uglyurls %}index.html{%- endif %}">{{i}}</a>
            {%- else -%}
            <a class="inp on" href="{{ paginator.first | safe }}{%- if uglyurls %}index.html{%- endif %}">{{ i }}</a>
            {%- endif -%}
        {%- endfor -%}

        {%- if paginator.number_pagers > paginator.current_index+5 and paginator.number_pagers > 7 %}
          <span class="dis outp">...</span>
          <a class="inp on" href="{{ paginator.last | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Last Page">{{paginator.number_pagers-1}}</a>
          <a class="inp on" href="{{ paginator.last | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Last Page">{{paginator.number_pagers}}</a>
        {%- elif paginator.number_pagers > paginator.current_index+4 and paginator.number_pagers > 7 %}
          <a class="inp on" href="{{ paginator.last | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Last Page">{{paginator.number_pagers-2}}</a>
          <a class="inp on" href="{{ paginator.last | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Last Page">{{paginator.number_pagers-1}}</a>
          <a class="inp on" href="{{ paginator.last | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Last Page">{{paginator.number_pagers}}</a>
        {%- elif paginator.number_pagers > paginator.current_index+3 and paginator.number_pagers > 6 %}
          <a class="inp on" href="{{ paginator.last | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Last Page">{{paginator.number_pagers-1}}</a>
          <a class="inp on" href="{{ paginator.last | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Last Page">{{paginator.number_pagers}}</a>
        {%- elif paginator.number_pagers > paginator.current_index+2 and paginator.number_pagers > 5 %}
          <a class="inp on" href="{{ paginator.last | safe }}{%- if uglyurls %}index.html{%- endif %}" title="Last Page">{{paginator.number_pagers}}</a>
        {%- endif -%}
      </nav>
  {%- endif %}
{%- endmacro %}
